package mlp.game.scootaloo;

import com.overkill.game.GameEngine;
import com.overkill.game.Renderable;
import com.overkill.game.Sprite;
import com.overkill.game.SpriteRenderable;

import android.graphics.Canvas;
import android.graphics.Paint;

public class Scootaloo extends SpriteRenderable {

	private SpriteRenderable wings;
	private float wingsOffsetX = 0;
	private float wingsOffsetY = 0;
	
	private Shadow shadow;
	
	private SpriteRenderable collect;
	private Paint collectPaint = new Paint();
	private float ground;
	public float jumpVelocity = 0;
	private float maxJumpVelocity = 0;
	private float maxJumpHeight = 0;
	public float velocity = 0;
	
	public boolean jumpRelease = false;	
	private boolean jumping = false;
	public boolean rightKey = false;
	public boolean leftKey = false;
	public boolean upKey = false;
	public boolean hover = false;
	public Renderable onTop = null;
	
	public Scootaloo(Sprite sprite, Sprite wings, float ground, Paint shadowPaint, float maxJumpVelocity) {
		super(sprite);
		this.wings = new SpriteRenderable(wings);
		this.ground = ground;
		moveTo(0, ground - getHeight());
		this.maxJumpVelocity = maxJumpVelocity;
		this.maxJumpHeight = GameEngine.dip(180);
		this.shadow = new Shadow(this, ground, maxJumpHeight, GameEngine.dip(20), GameEngine.dip(5.3f), shadowPaint);
		this.shadow.setSize(getWidth() * 1.05f, GameEngine.dip(16));
	}
	
	public void setWingsOffset(float x, float y){
		this.wingsOffsetX = x;
		this.wingsOffsetY = y;
	}
	
	@Override
	public void update(long gameTime) {
		updateVelocity(gameTime);
		updateScootaloo(gameTime);
		this.shadow.update();
		super.update(gameTime);
		wings.update(gameTime);
		if(collect != null){
			collect.moveYBy(-2);
			int a = collectPaint.getAlpha();
			a -= 10;
			if(a > 0){
				collectPaint.setAlpha(a);
			} else {
				collect = null;
			}
		}
	}
	
	public void collect(SpriteRenderable collect){
		this.collect = collect;
		this.collect.moveX(getFrame().centerX() - collect.getWidth() / 2 + GameEngine.dip(26.6f));
		this.collect.moveY(getFrame().top - collect.getHeight());
		this.collectPaint.setAlpha(255);
	}
	
	private void updateVelocity(long gameTime){		
		velocity = 0;
		if(rightKey == true){
			velocity = GameEngine.dip(7);
		}
		if(leftKey == true && getPosition().x > 0){
			velocity = -GameEngine.dip(7);
		}
		if(jumpVelocity < -maxJumpVelocity){
			jumpVelocity = -maxJumpVelocity;
//			if(ground - (getPosition().y + getHeight()) > maxJumpHeight){
				//jumpRelease = false;
//			}
		}
		if(jumpVelocity > maxJumpVelocity){
			jumpVelocity = maxJumpVelocity;
		}
		// Up
		if(upKey == true && jumpRelease == true){
			if(jumping == false){
				jumping = true;
				jumpVelocity = -maxJumpVelocity;
			} else if(jumpVelocity <= 0)
				jumpVelocity += GameEngine.dip(0.5f);
			else{
				jumpRelease = false;
			}
			//jumpVelocity = jumpVelocity - 5; //((ground - getHeight() - getPosition().y) / maxJumpHeight / 1);
		}
		// Down
		if(getPosition().y + getHeight() < ground && jumpRelease == false){
			if (upKey == true) {
				if (jumpVelocity >= 0) {
					jumpVelocity = jumpVelocity + GameEngine.dip(0.0033f);
				} else {
					jumpVelocity += GameEngine.dip(1);
				}
//			} else if (jumpVelocity >= 0) {
//				jumpVelocity = jumpVelocity + 0.5f;
			} else {
				jumpVelocity += GameEngine.dip(2);
			}
			/*if(hover == true && jumpVelocity > 0){
				jumpVelocity = jumpVelocity + GameEngine.dip(0.0033f); //0.05f;
			}else{
				jumpVelocity = jumpVelocity + 5; //((ground - getHeight() - getPosition().y) / maxJumpHeight / 1);
			}*/
		}
	}
	
	public void resetJump(){
		jumpRelease = true;
		jumping = false;
		jumpVelocity = 0;
	}
	
	private void updateScootaloo(long gameTime){
		// Falling below ground
		if(getPosition().y + getHeight() + jumpVelocity > ground){
			moveY(ground - getHeight());
			jumpVelocity = 0;
			jumpRelease = true;
			jumping = false;
		}
		// On ground
		if(getPosition().y + getHeight() == ground){
			jumpRelease = true;
		}
		// Not moving out of left frame border
		if(getPosition().x + velocity < 0){
			moveX(0);
			velocity = 0;
		}
		// Not moving out of right frame border
		if(getPosition().x + velocity > (GameEngine.getStage().width() * 1.0f) - (getWidth() * 1.0f)){
			moveX((GameEngine.getStage().width() * 1.0f) - (getWidth() * 1.0f));
			velocity = 0;
		}else{
			moveXBy(velocity);
		}
		if(onTop != null && jumpVelocity >= 0){
			//moveYBy(0);		
			moveY(onTop.getFrame().top - getHeight());			
		}else{
			moveYBy(jumpVelocity);
		}
	}
	
	@Override
	public void moveTo(float x, float y) {
		super.moveTo(x, y);
		wings.moveTo(x + wingsOffsetX, y + wingsOffsetY);
		if(collect != null){
			collect.moveX(getFrame().centerX() - collect.getWidth() / 2 + GameEngine.dip(26.6f));
		}
	}
	
	@Override
	public void draw(Canvas canvas) {
		this.shadow.draw(canvas);
		if(collect != null){
			collect.draw(canvas, collectPaint);
		}
		super.draw(canvas);
		this.wings.draw(canvas);
	}
	
	public boolean isOnTop(Renderable r){		
		boolean b1 = getFrame().right >= r.getFrame().left && getFrame().left <= r.getFrame().right;
		boolean b2 = getFrame().bottom <= r.getFrame().top;
		return b1 && b2;
	}
	
}
